using System.Drawing;
using OpenComposite.Properties;

namespace OpenComposite.Base.Designer
{
	public class CollapseButton : Button
	{
		#region Initialization
		public CollapseButton()
		{
			this.BeginUpdate();

			_bmpSign = null;
			_isCollapsed = false;

			this.CollapseSign = CollapseSign.PlusMinus;

			this.Size = new System.Drawing.Size(16, 16);
			this.Border = BorderType.None;
			this.BackBrushMode = BackgroundBrushMode.Solid;
			this.BackColor = Color.Transparent;
			this.MouseOverColor = Color.Transparent;
			this.Text = "";
			this.NameTextBox.Visible = false;

			this.EndUpdate();
		}
		#endregion

		#region Properties
		public override bool NoClip
		{
			get
			{
				return true;
			}
		}
		public bool IsCollapsed
		{
			get { return _isCollapsed; }
			set
			{
				if ( _isCollapsed != value ) {
					_isCollapsed = value;
					setSignBitmap();
					this.Refresh();
				}
			}
		}
		public CollapseSign CollapseSign
		{
			get { return _collapseSign; }
			set
			{
				if ( _collapseSign != value ) {
					_collapseSign = value;
					if ( _collapseSign == CollapseSign.None )
						this.NameTextBox.Visible = true;
					else
						this.NameTextBox.Visible = false;
					setSignBitmap();
					this.Refresh();
				}
			}
		}
		#endregion

		#region Variables
		private CollapseSign _collapseSign;
		private bool _isCollapsed;
		private Bitmap _bmpSign;
		#endregion

		#region Methods
		public override void Paint(System.Windows.Forms.PaintEventArgs pe)
		{
			if ( _bmpSign != null ) {
				Rectangle r = this.GetPaintBounds();
				pe.Graphics.DrawImageUnscaled(_bmpSign, r);
			}
			base.Paint(pe);
		}

		private void setSignBitmap()
		{
			switch ( _collapseSign ) {
				case CollapseSign.PlusMinus:
					if ( this.IsCollapsed )
						_bmpSign = Resources.bmp_collapsed;
					else
						_bmpSign = Resources.bmp_expanded;
					break;
				case CollapseSign.DoubleArrow:
					if ( this.IsCollapsed )
						_bmpSign = Resources.bmp_collapse_large;
					else
						_bmpSign = Resources.bmp_expand_large;
					break;
				case CollapseSign.SingleArrow:
					if ( this.IsCollapsed )
						_bmpSign = Resources.bmp_collapse_small;
					else
						_bmpSign = Resources.bmp_expand_small;
					break;
			}
			if ( _bmpSign != null ) {
				_bmpSign.MakeTransparent(Color.Magenta);
			} else {
				this.NameTextBox.Visible = true;
			}
		}
		#endregion
	}

	#region Enum: CollapseSign
	public enum CollapseSign
	{
		None = 0,
		PlusMinus,
		DoubleArrow,
		SingleArrow
	}
	#endregion

}
